﻿using Dapper;
using HongStone.Dto;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HongStone.Repository
{
    public class EquipmentDal
    {
        private readonly HongStoneContext _hongStoneContext;
        private readonly IConfiguration _configuration;
        public EquipmentDal(HongStoneContext hongStoneContext, IConfiguration configuration)
        {
            _hongStoneContext = hongStoneContext;
            _configuration = configuration;
        }
        /// <summary>
        /// 绑定楼层
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ResultData BindHome(int id)
        {
            var list = _hongStoneContext.homes.Where(m => m.CodeId == id).ToList();
            return new ResultData() { Code = 1, Data = list };
        }
        /// <summary>
        /// 显示消防风机
        /// </summary>
        /// <param name="louid"></param>
        /// <param name="danid"></param>
        /// <param name="cengid"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public ResultData ShowFireFan(int louid,int danid,int cengid,int state,string homenumber)
        {
            using (var conn = new SqlConnection(_configuration.GetConnectionString("MSSQL")))
            {
                var sql = $"select a.*,b.HomeName as LouName,c.HomeName as DanName,d.HomeName as CengName from FireFan a join Home b on a.LouId=b.HomeId join Home c on a.DanId=c.HomeId join Home d on a.CengId=d.HomeId where 1=1 ";
                if(louid>0)
                {
                    sql += $" and a.LouId='{louid}'";
                }
                if (danid > 0)
                {
                    sql += $" and a.DanId='{danid}'";
                }
                if (cengid > 0)
                {
                    sql += $" and a.CengId='{cengid}'";
                }
                if (state > -1)
                {
                    sql += $" and a.State='{state}'";
                }
                if (!string.IsNullOrWhiteSpace(homenumber))
                {
                    sql += $" and a.HomeNumber='{homenumber}'";
                }
                var list = conn.Query<FireFanDto>(sql).ToList();
                return new ResultData() { Code = 1, Data = list };
            }
        }
    }
}
